﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Alos.Entity;
using Alos.IRepository;
using PetaPoco;

namespace Alos.Repository
{
   public class SitPosSettingRepository: BaseRepository<SitPosSetting>, ISitPosSettingRepository
    {
        /// <summary>
        /// 根据老师ID和班级ID获取学生座位顺序
        /// </summary>
        /// <param name="TeacherId">老师ID</param>
        /// /// <param name="classid">班级ID</param>
        /// <returns></returns>
       public SitPosSetting GetSitPosSetting(int TeacherId, int classid)
       {
           var sql = Sql.Builder.Append("  select * from SitPosSetting where TeacherId=@0  and ClassId=@1 ", TeacherId, classid);
           return DBContext.SingleOrDefault<SitPosSetting>(sql);
       }
         ///<summary>
         ///根据老师ID和班级ID修改学生座位顺序
         ///</summary>
         ///<param name="TeacherId">老师ID</param>
         ///<param name="classid">班级ID</param>
         ///<param name="colnum">有几列</param>
         ///<param name="stus">学生ID以,分割</param>
         ///<returns></returns>
       //public bool UpdateSitPosSetting(int TeacherId, int classid, int colnum, string stus)
       //{
       //    var sql = Sql.Builder.Append("update SitPosSetting set ColNum=@2,SequStduIds=@3 where TeacherId=@0 and ClassId=@1 ", TeacherId, classid, colnum, stus);
       //    return DBContext.Execute(sql) > 0;
       ////}

       public bool UpdateSitPosSetting(int TeacherId, int classid, int colnum, string stus)
       {
           //var sql = Sql.Builder.Append("select * from SitPosSetting where TeacherId=@0 and ClassId=@1", TeacherId, classid);
           //SitPosSetting Sit = DBContext.SingleOrDefault<SitPosSetting>(sql);
           //if (Sit!=null)
           //{
           //    sql = Sql.Builder.Append("update SitPosSetting set ColNum=@2,SequStduIds=@3 where TeacherId=@0 and ClassId=@1",TeacherId,classid,colnum,stus);
           //    return DBContext.Execute(sql) > 0;
           //}
           //else
           //{
           //    sql = Sql.Builder.Append("insert into SitPosSetting(TeacherId,ClassId,ColNum,SequStduIds) values(@0,@1,@2,@3)", TeacherId, classid, colnum, stus);
           //    return DBContext.Execute(sql) > 0;
           //}
           var sql = Sql.Builder.Append("if not exists(select * from SitPosSetting where TeacherId=@0 and ClassId=@1) begin insert into SitPosSetting(TeacherId,ClassId,ColNum,SequStduIds) values(@0,@1,@2,@3) end else begin update SitPosSetting set ColNum=@2,SequStduIds=@3 where TeacherId=@0 and ClassId=@1 end", TeacherId, classid, colnum, stus);
           return DBContext.Execute(sql) > 0;

       }
    }
}
